<!--
 * @Description: 
 * @Version: 1.0
 * @Author: Shi Hexiang
 * @Date: 2023-09-25 15:31:11
 * @LastEditors: Shi Hexiang
 * @LastEditTime: 2023-09-27 17:51:15
-->
<template>
  <div class="logstyle">
    <el-card>
      <el-form :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item label="操作人员">
          <el-input v-model="formInline.name" placeholder="操作人员" clearable></el-input>
        </el-form-item>
        <el-form-item label="操作时间">
          <el-date-picker
            v-model="formInline.time"
            type="datetimerange"
            value-format="YYYY-MM-DD HH:mm:ss"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" size="small" @click="onSubmit" :icon="Search">查询</el-button>
          <el-button size="small" @click="onRefresh" :icon="Refresh">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card class="logstyle-table">
      <!-- <el-button v-auth="'system:user:add'" type="primary" size="small" :icon="Plus" @click="onAdd">新增</el-button> -->
      <el-table ref="multipleTable" :data="tableData" tooltip-effect="dark" style="width: 100%; margin-top: 20px" border>
        <el-table-column type="index" label="序号" width="80"></el-table-column>
        <el-table-column prop="id" label="日志编号"> </el-table-column>
        <el-table-column prop="op" label="操作类型"> </el-table-column>
        <el-table-column prop="uid" label="操作人"> </el-table-column>
        <el-table-column prop="ip" label="ip地址"> </el-table-column>
        <el-table-column prop="type" label="操作状态">
          <template #default="scope">
            <div :style="{ color: scope.row.result === 1 ? '#67C23A' : '#F56C6C' }">
              {{ scope.row.result === 1 ? "成功" : "失败" }}
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="dateline" label="操作日期"> </el-table-column>
        <el-table-column prop="costTime" label="请求耗时（ms）" />
        <el-table-column label="操作">
          <template #default="scope">
            <el-button @click="handleLookClick(scope.row)" link type="primary" size="small">查看</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <el-pagination
      class="pagination"
      :current-page="page.pageIndex"
      :page-size="page.pageSize"
      :page-sizes="[5, 15, 30, 50]"
      background
      layout="sizes, prev, pager, next"
      :total="page.totalSize"
      @size-change="handleSizeChange"
      @current-change="changeCurrent"
    />
    <el-dialog :title="title" v-model="isDialog" width="700px" :close-on-click-modal="false">
      <el-row class="logstyle-margin">
        <el-col :span="12">
          <span class="colstyle">操作模块：</span>
          <span>{{ logDetail?.module }}</span>
        </el-col>
        <el-col :span="12">
          <span class="colstyle">请求地址：</span>
          <span>{{ logDetail?.path }}</span>
        </el-col>
      </el-row>
      <el-row class="logstyle-margin">
        <el-col :span="12">
          <span class="colstyle">登录信息：</span>
          <span>{{ logDetail?.uid }} / {{ logDetail?.ip }}</span>
        </el-col>
        <el-col :span="12">
          <span class="colstyle">操作状态：</span>
          <span>{{ logDetail?.result === 1 ? "成功" : "失败" }}</span>
        </el-col>
      </el-row>
      <el-row class="logstyle-margin">
        <el-col :span="24">
          <span class="colstyle">请求参数：</span>
          <span>{{ logDetail?.request }}</span>
        </el-col>
      </el-row>
      <el-row class="logstyle-margin">
        <el-col :span="24">
          <span class="colstyle">返回参数：</span>
          <span>{{ logDetail?.response }}</span>
        </el-col>
      </el-row>
      <el-row class="logstyle-margin">
        <el-col :span="12">
          <span class="colstyle">消耗时间：</span>
          <span>{{ logDetail?.costTime }}ms</span>
        </el-col>
        <el-col :span="12">
          <span class="colstyle">操作时间：</span>
          <span>{{ logDetail?.dateline }}</span>
        </el-col>
      </el-row>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="cancelBtn" size="small">关 闭</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { Search, Refresh } from "@element-plus/icons-vue"
import { onMounted, ref } from "vue"
import { SysRes } from "@/api/interface/system/response"
import { SysReq } from "@/api/interface/system/request"
import { getLogPageListApi } from "@/api/modules/system"
import type { ElForm } from "element-plus"
const tableData = ref<SysRes.LogPageList[]>([])
const formInline = ref<{ name: string; time: string[] }>({
  name: "",
  time: []
})
const logDetail = ref<SysRes.LogPageList>()
const title = ref("新增")
const isDialog = ref(false)
const page = ref<{ [key: string]: number }>({
  // 当前页
  pageIndex: 1,
  // 当前页吗的数量
  pageSize: 5,
  // 总数
  totalSize: 0
})

onMounted(() => {
  onSearch()
})
/**分页每页大小 */
const handleSizeChange = (val: number) => {
  console.log(val)
  page.value.pageSize = val
  onSearch()
}
/**分页 */
const changeCurrent = (val: number) => {
  page.value.pageIndex = val
  onSearch()
}
/** 查看*/
const handleLookClick = (row: SysRes.LogPageList) => {
  isDialog.value = true
  logDetail.value = row
}

/**查询列表 */
const onSearch = async () => {
  let parmas = {
    uid: formInline.value.name,
    startTime: formInline.value.time.length === 0 ? "" : formInline.value.time[0],
    endTime: formInline.value.time.length === 0 ? "" : formInline.value.time[1],
    pageIndex: page.value.pageIndex,
    pageSize: page.value.pageSize
  }
  const { data } = await getLogPageListApi(parmas as SysReq.LogPageList)
  tableData.value = data.result
  page.value.totalSize = data.totalCount
}

/**重置 */
const onRefresh = () => {
  formInline.value.name = ""
  formInline.value.time = []
  onSearch()
}
/**查询按钮 */
const onSubmit = () => {
  onSearch()
}
/**取消弹出框 */
const cancelBtn = () => {
  isDialog.value = false
}
</script>

<style scoped lang="scss">
.logstyle {
  .logstyle-table {
    margin-top: 10px;
  }
  .pagination {
    float: right;
    margin-top: 20px;
  }
  .logstyle-width {
    width: 220px;
  }
  .logstyle-margin {
    margin-top: 20px;
    font-size: 15px;
  }
  .colstyle {
    margin-right: 10px;
    font-weight: bold;
  }
}
</style>
